package com.zhdl.modules.demo.DCHTProcess.camera;

import com.zhdl.common.ytools.BaseConversionTools;
import com.zhdl.hardware_contact.hardware_config.entity.ProjectConfiguration;
import com.zhdl.modules.process.common.GetPLCValue;
import com.zhdl.network.strategy.common_server.impl.CommonServerSendServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CameraInterface implements GetPLCValue {

    public final static String CAMERA_CLIENT_IP = "192.168.1.50";
    public final static String CAMERA_SERVER_IP = "192.168.1.101:3000";


    //默认相机坐标
    public static final String DEF_LOCATION = "0,0,0";
    //默认值
    public static final String DEF_BOOL = "0";

    //读取的对象
    public List<ProjectConfiguration> configurationPLcList;

    //相机变量
    public final static String CAMERA1_TAKE_PHOTO1 = "机器人拍照位1";
    public final static String CAMERA1_TAKE_PHOTO2 = "机器人拍照位2";
	public final static String CAMERA1_PHOTO_RESULT = "机器人拍照结果";

	public final static String CAMERA2_TAKE_PHOTO1 = "触头拍照位1";
	public final static String CAMERA2_TAKE_PHOTO2 = "触头拍照位2";
	public final static String CAMERA2_PHOTO_RESULT = "触头拍照结果";

    public final static String CAMERA_CALIB_RESULT = "相机标定结果";



    public static final String CAMERA_RESULT1 = "触头拍照位1";
    public static final String CAMERA_RESULT2 = "拍照结果";

    public static final String DO_LUOSI_COMPLETE = "打螺丝机构完成";




    /**
     * 相机标定-机器人坐标坐标 X,Y,R
     */
    float[][] robotCoordinate = new float[12][3];

    @Resource
    private CommonServerSendServiceImpl commonServerSendService;

    /**
     * 初始化数据读取
     */
    public void initData(){
        configurationPLcList = new ArrayList<>();
//        632.872,251.314,90
//        602.872,221.314
        robotCoordinate[0][0] = 602.872f;
        robotCoordinate[0][1] = 221.314f;
        robotCoordinate[0][2] = 90.0f;
        for (int i = 0; i < 11; i++) {
            if (i==0){
                robotCoordinate[i+1][0] =  robotCoordinate[0][0] + 30.f;
                robotCoordinate[i+1][1] =  robotCoordinate[0][1];
            }
            if (i ==1 ){
                robotCoordinate[i+1][0] =  robotCoordinate[0][0] + 60f;
                robotCoordinate[i+1][1] =  robotCoordinate[0][1] ;
            }


            if (i == 2){
                robotCoordinate[i+1][0] =  robotCoordinate[0][0] + 60f;
                robotCoordinate[i+1][1] =  robotCoordinate[0][1] ;
            }
            if (i == 3){
                robotCoordinate[i+1][0] =  robotCoordinate[0][0] + 30f;
                robotCoordinate[i+1][1] =  robotCoordinate[0][1] + 30f;
            }

            if (i == 4){
                robotCoordinate[i+1][0] =  robotCoordinate[0][0] ;
                robotCoordinate[i+1][1] =  robotCoordinate[0][1] + 30f;
            }
            if (i == 5){
                robotCoordinate[i+1][0] =  robotCoordinate[0][0] ;
                robotCoordinate[i+1][1] =  robotCoordinate[0][1] + 60f;
            }
            if (i == 6){
                robotCoordinate[i+1][0] =  robotCoordinate[0][0] + 30f ;
                robotCoordinate[i+1][1] =  robotCoordinate[0][1] + 60f;
            }
            if (i == 7){
                robotCoordinate[i+1][0] =  robotCoordinate[0][0] + 60f ;
                robotCoordinate[i+1][1] =  robotCoordinate[0][1] + 60f;
            }
            if (i == 8){
                robotCoordinate[i+1][0] =  robotCoordinate[0][0] + 30f;
                robotCoordinate[i+1][1] =  robotCoordinate[0][1] + 30f;
            }
            if (i == 9){
                robotCoordinate[i+1][0] =  robotCoordinate[0][0] + 30f;
                robotCoordinate[i+1][1] =  robotCoordinate[0][1] + 30f;
            }
            if (i == 10){
                robotCoordinate[i+1][0] =  robotCoordinate[0][0] + 30f;
                robotCoordinate[i+1][1] =  robotCoordinate[0][1] + 30f;
            }

            if (i == 9){
                robotCoordinate[i+1][2] = 90 - 10f;
            }else if (i == 10){
                robotCoordinate[i+1][2] = 90 + 10f;
            }else{
                robotCoordinate[i+1][2] = 90;
            }
        }

        //机器人-相机初始化
        configurationPLcList.add(new ProjectConfiguration(CAMERA1_TAKE_PHOTO1,DEF_BOOL,1));
        configurationPLcList.add(new ProjectConfiguration(CAMERA1_TAKE_PHOTO2,DEF_BOOL,1));
        configurationPLcList.add(new ProjectConfiguration(CAMERA1_PHOTO_RESULT,DEF_LOCATION,4));

        //打螺丝位-相机初始化
        configurationPLcList.add(new ProjectConfiguration(CAMERA2_TAKE_PHOTO1,DEF_BOOL,1));
        configurationPLcList.add(new ProjectConfiguration(CAMERA2_TAKE_PHOTO2,DEF_BOOL,1));
        configurationPLcList.add(new ProjectConfiguration(CAMERA2_PHOTO_RESULT,DEF_LOCATION,4));

        //机器人-相机标定结果
        for (int i = 0; i < 12; i++) {
            configurationPLcList.add(new ProjectConfiguration(CAMERA_CALIB_RESULT + i ,DEF_LOCATION,1));
        }

        configurationPLcList.add(new ProjectConfiguration("相机在线状态",DEF_BOOL,4));

        //打螺丝位-螺丝机构
        configurationPLcList.add(new ProjectConfiguration("打螺丝机构完成",DEF_BOOL,1));

        //相机回调
        commonServerSendService.setDataCallBack(CAMERA_SERVER_IP, s -> {
            log.info("当前机器人相机返回数据:{},Ascii数据:{}",s,BaseConversionTools.hexStringToAscii(s) );
            //处理相机数据
            cameraDataHandle(BaseConversionTools.hexStringToAscii(s));
        });
    }

    /**
     * 相机数据处理
     * @param result 十六进制数据
     */
    public void cameraDataHandle(String result){
        String[] cameraDatas = result.split(",");
        //获取本次指令的类型
        cameraDatas[0] = cameraDatas[0].trim();
        String type = cameraDatas[0];
        switch (type) {
            //机器人相机-标定
            case "Calib" ->{
                if (cameraDatas[2].trim().equals("OK")){
                    upDataValue(CAMERA_CALIB_RESULT + cameraDatas[1].trim(),"1");
                } else {
                    upDataValue(CAMERA_CALIB_RESULT + cameraDatas[1].trim(),"2");
                }
            }
            //工位1拍照
            case "T101" ->{
                upDataValue(CAMERA1_TAKE_PHOTO1,"1");
                if (cameraDatas[2].trim().equals("OK")){
                    upDataValue(CAMERA_CALIB_RESULT,"ok");
                } else {
                    upDataValue(CAMERA_CALIB_RESULT,"ng");
                }
            }
            case "T102" ->{
                upDataValue(CAMERA1_TAKE_PHOTO2,"1");
                if (cameraDatas[2].trim().equals("OK")){
                    upDataValue(CAMERA1_PHOTO_RESULT,cameraDatas[3] + "," + cameraDatas[4] + "," + cameraDatas[5]);
                } else {
                    upDataValue(CAMERA1_PHOTO_RESULT,"ng");
                }
            }
            //工位2拍照
            case "T201" ->{
                upDataValue(CAMERA2_TAKE_PHOTO1,"1");
                if (cameraDatas[2].trim().equals("OK")){
                    upDataValue(CAMERA2_PHOTO_RESULT ,"ok");
                } else {
                    upDataValue(CAMERA2_PHOTO_RESULT,"ng");
                }
            }
            case "T202" ->{
                upDataValue(CAMERA2_TAKE_PHOTO2,"1");
                if (cameraDatas[2].trim().equals("OK")){
                    upDataValue(CAMERA2_PHOTO_RESULT,cameraDatas[3] + "," + cameraDatas[4] + "," + cameraDatas[5]);
                } else {
                    upDataValue(CAMERA2_PHOTO_RESULT,"ng");
                }
            }
            //心跳
            case "Heart" ->{
                //回复心跳
//                heartAction();

                ProjectConfiguration cameraOnline = getValue("相机在线状态");
                cameraOnline.setValue("1");
                cameraOnline.setCreateDate(String.valueOf(System.currentTimeMillis()));
            }
            default -> throw new IllegalStateException("Unexpected value: " + type);
        }
    }

    /**
     * 相机心跳的动作
     */
    public void heartAction() {
        commonServerSendService.sendToClientCmd(CAMERA_SERVER_IP,CAMERA_CLIENT_IP,"Heart,1");
    }
    public void reSet(){
        for (ProjectConfiguration projectConfiguration : configurationPLcList) {
            if (projectConfiguration.valueType == 1) {
                projectConfiguration.value = "0";
            }
        }
    }
    @Override
    public ProjectConfiguration getValue(String field) {
        for (ProjectConfiguration configurationPLc : configurationPLcList) {
            if (configurationPLc.getField().equals(field)){
                return configurationPLc;
            }
        }
        return null;
    }
    public void upDataValue(String field,String value){
        for (ProjectConfiguration configurationPLc : configurationPLcList) {
            if (configurationPLc.getField().equals(field)){
                configurationPLc.setValue(value);
            }
        }
    }
}
